#---------------------------------------------------------------------------------
# Clear the implicit built in rules
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------
ifeq ($(strip $(DEVKITPPC)),)
$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=<path to>devkitPPC")
endif

#include $(DEVKITPPC)/gamecube_rules
include $(DEVKITPPC)/wii_rules

#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# INCLUDES is a list of directories containing extra header files
#---------------------------------------------------------------------------------
TARGET		:=	stepmania
BUILD		:=	output
SOURCES		:=	. \
		arch \
		arch/ArchHooks \
		arch/Dialog \
		arch/InputHandler \
		arch/Lights \
		arch/LoadingWindow \
		arch/LowLevelWindow \
		arch/MemoryCard \
		arch/MovieTexture \
		arch/Sound \
		arch/Threads \
		archutils/Wii \
		ffmpeg \
		crypto \
		crypto51 \
		pcre 
DATA		:=	
INCLUDES	:= . \
		arch \
		arch/ArchHooks \
		arch/Dialog \
		arch/InputHandler \
		arch/Lights \
		arch/LoadingWindow \
		arch/LowLevelWindow \
		arch/MemoryCard \
		arch/MovieTexture \
		arch/Sound \
		arch/Threads \
		archutils/Wii \
		ffmpeg \
		crypto \
		crypto51 \
		pcre
		
DEFINE		:= -DHAVE_CONFIG_H

#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------

CFLAGS	= -g -O2 -Wall -Wno-switch $(MACHDEP) $(INCLUDE) $(DEFINE)
CXXFLAGS	=	$(CFLAGS)

LDFLAGS	=	-g $(MACHDEP) -Wl,-Map,$(notdir $@).map

#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project
#---------------------------------------------------------------------------------
LIBS	:= -lfat -logc -lm -ljpeg -lpng -lz -llua -lresample \
			-lwiiuse -lbte 

#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS	:= $(DEVKITPPC)

#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------

export OUTPUT	:=	$(CURDIR)/$(TARGET)

export VPATH	:=	$(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
					$(foreach dir,$(DATA),$(CURDIR)/$(dir))

export DEPSDIR	:=	$(CURDIR)/$(BUILD)

#---------------------------------------------------------------------------------
# automatically build a list of object files for our project
#---------------------------------------------------------------------------------
CFILES		:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES	:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
sFILES		:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
SFILES		:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S)))
BINFILES	:=	$(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))

#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
	export LD	:=	$(CC)
else
	export LD	:=	$(CXX)
endif

#----------------------------------------------------------------
# StepMania objects
#----------------------------------------------------------------
Screens = Screen.o ScreenAttract.o ScreenBookkeeping.o ScreenBranch.o ScreenCaution.o ScreenCenterImage.o \
	ScreenCredits.o ScreenDemonstration.o ScreenClearBookkeepingData.o ScreenClearMachineStats.o \
	ScreenDownloadMachineStats.o ScreenEdit.o ScreenEditCoursesMenu.o EditCoursesSongMenu.o \
	ScreenEditMenu.o ScreenEnding.o ScreenEndlessBreak.o ScreenEvaluation.o ScreenExit.o \
	ScreenNetEvaluation.o ScreenNetSelectMusic.o ScreenEz2SelectMusic.o \
	ScreenEz2SelectPlayer.o ScreenGameOver.o ScreenGameplay.o ScreenHowToPlay.o ScreenInstructions.o \
	ScreenInsertCredit.o ScreenJukebox.o ScreenJukeboxMenu.o ScreenLogo.o \
	ScreenMapControllers.o ScreenMiniMenu.o ScreenMusicScroll.o ScreenNameEntry.o \
	ScreenNameEntryTraditional.o ScreenOptions.o ScreenOptionsMaster.o ScreenOptionsMasterPrefs.o \
	ScreenPlayerOptions.o ScreenNetworkOptions.o ScreenProfileOptions.o ScreenPrompt.o \
	ScreenRanking.o ScreenReloadSongs.o ScreenResetToDefaults.o ScreenSandbox.o \
	ScreenSelect.o ScreenSelectCharacter.o ScreenSelectDifficulty.o ScreenSelectGroup.o \
	ScreenSelectMaster.o ScreenSelectMode.o ScreenSelectMusic.o ScreenSelectStyle.o \
	ScreenSetTime.o ScreenSongOptions.o ScreenStage.o ScreenStyleSplash.o \
	ScreenTest.o ScreenTestFonts.o ScreenTestInput.o ScreenTestLights.o \
	ScreenTestSound.o ScreenTextEntry.o ScreenTitleMenu.o ScreenUnlock.o \
	ScreenWithMenuElements.o

DataStructures = Attack.o BannerCache.o CatalogXml.o Character.o CharacterHead.o CodeDetector.o \
	Difficulty.o EnumHelper.o Course.o CourseUtil.o DateTime.o Font.o \
	FontCharAliases.o FontCharmaps.o Game.o GameConstantsAndTypes.o GameInput.o \
	Grade.o HighScore.o Inventory.o LuaHelpers.o LyricsLoader.o \
	ModeChoice.o NoteData.o NoteDataUtil.o NoteDataWithScoring.o NoteFieldPositioning.o \
	NoteTypes.o NotesLoader.o NotesLoaderBMS.o NotesLoaderDWI.o \
	NotesLoaderKSF.o NotesLoaderSM.o NotesWriterDWI.o NotesWriterSM.o \
	PlayerAI.o PlayerNumber.o PlayerOptions.o Profile.o RandomSample.o RadarValues.o \
	ScoreKeeperMAX2.o ScoreKeeperRave.o Song.o SongCacheIndex.o SongOptions.o SongUtil.o \
	StageStats.o Steps.o StepsUtil.o Style.o StyleUtil.o TimingData.o Trail.o TrailUtil.o \
	TitleSubstitution.o

FileTypes = IniFile.o MsdFile.o XmlFile.o
StepMania = StepMania.o global.o
Sound = RageSoundDriver_Null.o RageSoundDriver_Generic_Software.o
ArchHooks = ArchHooks.o ArchHooks_Wii.o
InputHandler = InputHandler.o InputHandler_Wii.o
MovieTexture = MovieTexture.o MovieTexture_Null.o \
	#MovieTexture_FFMpeg.cpp 
	
Lights = LightsDriver.o LightsDriver_SystemMessage.o 
MemoryCard = MemoryCardDriver.o MemoryCardDriverThreaded.o

ArchUtils = AssertionHandler.o GetSysInfo.o SignalHandler.o \
	EmergencyShutdown.o Backtrace.o BacktraceNames.o \
	CrashHandler.o CrashHandlerChild.o

Dialog = Dialog.o
Threads = Threads_Wii.o

Arch = \
	$(Sound) $(ArchHooks) $(InputHandler) $(MovieTexture) \
	$(Lights) $(MemoryCard) $(ArchUtils) $(Dialog) $(Threads) \
	arch.o
	
ActorsInGameplayAndMenus = BGAnimation.o BGAnimationLayer.o Banner.o \
	ConditionalBGA.o DifficultyIcon.o MeterDisplay.o Transition.o

ActorsInMenus = ActiveAttackList.o BPMDisplay.o ComboGraph.o CourseContentsList.o \
	CourseEntryDisplay.o DifficultyDisplay.o DifficultyList.o DifficultyMeter.o \
	DifficultyRating.o DualScrollBar.o EditCoursesMenu.o EditMenu.o \
	FadingBanner.o GradeDisplay.o GraphDisplay.o GrooveGraph.o \
	GrooveRadar.o GroupList.o HelpDisplay.o JukeboxMenu.o ListDisplay.o \
	MemoryCardDisplay.o MenuTimer.o ModeSwitcher.o MusicBannerWheel.o \
	MusicList.o MusicSortDisplay.o MusicWheel.o MusicWheelItem.o \
	OptionIcon.o OptionIconRow.o OptionsCursor.o PaneDisplay.o \
	ScrollBar.o ScrollingList.o SongCreditDisplay.o SnapDisplay.o \
	TextBanner.o WheelNotifyIcon.o

ActorsInGameplay = 	ArrowBackdrop.o ArrowEffects.o AttackDisplay.o Background.o \
	BeginnerHelper.o CombinedLifeMeterTug.o Combo.o DancingCharacters.o \
	Foreground.o GhostArrow.o GhostArrowRow.o HoldGhostArrow.o \
	HoldJudgment.o Judgment.o LifeMeterBar.o LifeMeterBattery.o \
	LyricDisplay.o NoteDisplay.o NoteField.o PercentageDisplay.o \
	Player.o ProTimingDisplay.o ReceptorArrow.o ReceptorArrowRow.o \
	ScoreDisplay.o ScoreDisplayBattle.o ScoreDisplayNormal.o ScoreDisplayOni.o \
	ScoreDisplayPercentage.o ScoreDisplayRave.o

Helpers = SDL_SaveJPEG.o SDL_dither.o SDL_rotozoom.o

PCRE = chartables.o get.o maketables.o pcre.o study.o

RageFile = 	RageFile.o RageFileDriver.o RageFileManager.o RageFileDriverDirect.o \
	RageFileDriverDirectHelpers.o RageFileDriverMemory.o RageFileDriverZip.o
	
RageSoundFileReaders = RageSoundReader_WAV.o \
	#RageSoundReader_Vorbisfile.o RageSoundReader_MP3.o

Rage = $(Helpers) $(PCRE) $(RageFile) $(RageSoundFileReaders) \
	RageBitmapTexture.o RageDisplay.o RageDisplay_Null.o RageException.o \
	RageInput.o RageInputDevice.o RageLog.o RageMath.o RageModelGeometry.o \
	RageSound.o RageSoundManager.o RageSoundPosMap.o RageSoundReader_FileReader.o \
	RageSoundReader_Preload.o RageSoundReader_Resample.o \
	RageSoundReader_Resample_Fast.o RageSoundReader_Resample_Good.o \
	RageSoundResampler.o RageSurface.o 	RageSurfaceUtils.o RageSurfaceUtils_Palettize.o \
	RageSurface_Load.o RageSurface_Load_PNG.o RageSurface_Load_JPEG.o RageSurface_Load_GIF.o \
	RageSurface_Load_BMP.o RageSurface_Load_XPM.o RageTexture.o RageSurface_Save_BMP.o \
	RageTextureID.o RageTextureManager.o RageThreads.o RageTimer.o RageUtil.o \
	RageUtil_CharConversions.o RageUtil_BackgroundLoader.o RageUtil_FileDB.o

Actors = Actor.o ActorCommands.o ActorFrame.o ActorScroller.o \
	ActorUtil.o BitmapText.o Model.o ModelManager.o ModelTypes.o Sprite.o

GlobalSingletons = AnnouncerManager.o Bookkeeper.o CryptManager.o FontManager.o \
	GameSoundManager.o GameManager.o GameState.o InputFilter.o InputMapper.o \
	InputQueue.o LightsManager.o MemoryCardManager.o NetworkSyncManager.o \
	NetworkSyncServer.o NoteSkinManager.o PrefsManager.o ProfileManager.o \
	ScreenManager.o SongManager.o ThemeManager.o UnlockSystem.o \
	#ezsockets.o

Crypto = CryptBn.o CryptMD5.o \
	CryptNoise.o CryptPrime.o CryptRSA.o \
	CryptRand.o CryptSH512.o CryptSHA.o

CryptoPp = algebra.o algparam.o asn.o cryptlib.o files.o filters.o \
	integer.o iterhash.o misc.o modes.o mqueue.o nbtheory.o \
	oaep.o osrng.o pkcspad.o pubkey.o queue.o randpool.o \
	rsa.o sha.o strciphr.o CryptHelpers.o

export OFILES := \
	$(Screens) $(DataStructures) $(FileTypes) $(StepMania) \
	$(Arch) $(ActorsInGameplayAndMenus) $(ActorsInMenus) \
	$(ActorsInGameplay) $(Rage) $(Actors) $(GlobalSingletons) \
	$(Crypto) $(CryptoPp)

#export OFILES	:=	$(addsuffix .o,$(BINFILES)) \
#					$(CPPFILES:.cpp=.o) $(CFILES:.c=.o) \
#					$(sFILES:.s=.o) $(SFILES:.S=.o)

#---------------------------------------------------------------------------------
# build a list of include paths
#---------------------------------------------------------------------------------
export INCLUDE	:=	$(foreach dir,$(INCLUDES), -iquote $(CURDIR)/$(dir)) \
					$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
					-I$(CURDIR)/$(BUILD) \
					-I$(LIBOGC_INC)

#---------------------------------------------------------------------------------
# build a list of library paths
#---------------------------------------------------------------------------------
export LIBPATHS	:=	$(foreach dir,$(LIBDIRS),-L$(dir)/lib) \
					-L$(LIBOGC_LIB)

export OUTPUT	:=	$(CURDIR)/$(TARGET)
.PHONY: $(BUILD) clean

#---------------------------------------------------------------------------------
$(BUILD):
	@[ -d $@ ] || mkdir -p $@
	@make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile

#---------------------------------------------------------------------------------
clean:
	@echo clean ...
	@rm -fr $(BUILD) $(OUTPUT).elf $(OUTPUT).dol

#---------------------------------------------------------------------------------
run:
	wiiload $(TARGET).dol
	
#---------------------------------------------------------------------------------
emulate:
	gcube $(TARGET).dol


#---------------------------------------------------------------------------------
else

DEPENDS	:=	$(OFILES:.o=.d)

#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT).dol: $(OUTPUT).elf
$(OUTPUT).elf: $(OFILES)

#---------------------------------------------------------------------------------
# This rule links in binary data with the .jpg extension
#---------------------------------------------------------------------------------
%.jpg.o	:	%.jpg
#---------------------------------------------------------------------------------
	@echo $(notdir $<)
	$(bin2o)

-include $(DEPENDS)

#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------
